package com.junzhiit.nengji.api.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.exception.InvalidArgumentException;
import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.api.service.OrdersGoodsService;
import com.junzhiit.nengji.api.service.OrdersService;
import com.junzhiit.nengji.api.web.util.APICodeStatus;
import com.junzhiit.nengji.api.web.vo.CreateOrderVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.model.enums.OrderType;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/orders")
public class OrdersController extends BaseController {

	@Autowired
	private OrdersService ordersService;
	
	@Autowired
	private OrdersGoodsService ordersGoodsService;

	@ApiOperation(value = "获取用户所有订单")
	@RequestMapping(method = RequestMethod.GET)
	public ResultVO orders(Integer pageNumber,Integer maxPageRow,Integer status,Integer type,String userId){
		AssertUtil.notNull(pageNumber, "pageNumber is required.");
		AssertUtil.notNull(maxPageRow, "maxPageRow is required.");
		AssertUtil.notBlank(userId, "userId is required.");
		return ordersService.findAllOrders(pageNumber,maxPageRow,status,type,userId);
	}
	
	@ApiOperation(value = "改变状态")
	@RequestMapping(value = "/change/status", method = RequestMethod.PUT)
	public ResultVO changeStatus(String id,Integer status) {
		AssertUtil.notBlank(id, "id is required.");
		AssertUtil.notNull(status, "status is required.");
		return ordersService.changeStatus(id,status);
	}
	
	@ApiOperation(value = "购物车-确认订单")
	@RequestMapping(value = "/{userId}/info/applets",method=RequestMethod.POST)
	public ResultVO findAppletsOrderInfo(String userId,String areaCode,@RequestParam("carts")List<String> carts){
		AssertUtil.notBlank(userId, "userId is required.");
		if(carts == null || carts.size()<1){
			throw new InvalidArgumentException("carts is required.");
		}
		return ordersService.findAppletsOrderInfo(userId,areaCode, carts);
	}
	
	@ApiOperation(value = "确认订单-线上")
	@RequestMapping(value = "/{userId}/info",method=RequestMethod.POST)
	public ResultVO findOrderInfo(String userId,String goods,String areaCode){
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(goods, "goods is required.");
		
		ResultVO result = new ResultVO();
		Map<String, Object> data = new  HashMap<String, Object>();
		ResultVO orderResult = ordersService.findOrderInfo(userId,goods,areaCode);
		if(orderResult.getStatus() == APICodeStatus.SUCCESS || orderResult.getStatus() == APICodeStatus.ADDRESS_IS_NULL){
			data.put("order", orderResult.getData());
		}else{
			result = orderResult;
		}
		data.put("dateTime", new Date());
		result.setData(data);
		return result;
	}
	
	@ApiOperation(value = "确认订单-线下")
	@RequestMapping(value = "/{userId}/info/shop",method=RequestMethod.POST)
	public ResultVO findShopOrderInfo(String userId,String goods){
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(goods, "goods is required.");
		
		ResultVO result = new ResultVO();
		Map<String, Object> data = new  HashMap<String, Object>();
		ResultVO orderResult = ordersService.findShopOrderInfo(userId,goods);
		if(orderResult.getStatus() == APICodeStatus.SUCCESS || orderResult.getStatus() == APICodeStatus.ADDRESS_IS_NULL){
			data.put("order", orderResult.getData());
		}else{
			result = orderResult;
		}
		data.put("dateTime", new Date());
		result.setData(data);
		return result;
	}
	
	@ApiOperation(value = "查看线上订单详情")
	@RequestMapping(value = "/info",method = RequestMethod.GET)
	public ResultVO orderInfo(String orderId){
		AssertUtil.notBlank(orderId, "orderId is required.");
		return ordersService.orderInfo(orderId);
	}
	
	@ApiOperation(value = "线下订单支付成功/查看线下订单详情")
	@RequestMapping(value = "/offline/info",method = RequestMethod.GET)
	public ResultVO offlinePaySuccess(String orderNumber){
		AssertUtil.notBlank(orderNumber, "orderNumber is required.");
		return ordersService.offlinePaySuccess(orderNumber);
	}
	
	@ApiOperation(value = "线上订单支付成功")
	@RequestMapping(value = "/online/success",method = RequestMethod.GET)
	public ResultVO onlinePaySuccess(String orderNumber){
		AssertUtil.notBlank(orderNumber, "orderNumber is required.");
		return ordersService.onlinePaySuccess(orderNumber);
	}
	
	@ApiOperation(value = "生成订单")
	@RequestMapping(value = "/order",method = RequestMethod.POST)
	public ResultVO order(HttpServletRequest request,CreateOrderVO order){
		AssertUtil.notNull(order.getUserId(), "userId is required.");
		AssertUtil.notNull(order.getShopId(), "shopId is required.");
		AssertUtil.notNull(order.getGoods(), "goods is required.");
		AssertUtil.notNull(order.getRealMoney(), "realMoney is required.");
		AssertUtil.notNull(order.getPayWay(), "payWay is required.");
		if(OrderType.DELIVERY.toIntValue() == order.getType()){
			AssertUtil.notNull(order.getAddressId(), "addressId is required.");
		}
		AssertUtil.notNull(order.getType(), "type is required.");
		AssertUtil.notNull(order.getPayMoney(), "payMoney is required.");
		AssertUtil.notNull(order.getTradeType(), "tradeType is required.");
		AssertUtil.notNull(order.getDiscountMoney(), "discountMoney is required.");
		return ordersService.createOrder(request, order);
	}
	
	@ApiOperation(value = "生成订单二维码")
	@RequestMapping(value = "/create/qrcode", method = RequestMethod.POST)
	public ResultVO createQRCode(String userId, String orderNumber) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(orderNumber, "orderNumber is required.");
		return ordersService.createQRCode(userId,orderNumber);
	}
	
	@ApiOperation(value = "根据订单ID查询未评价的订单商品")
	@RequestMapping(value = "/goods",method = RequestMethod.GET)
	public ResultVO getOrderGoods(String orderId){
		AssertUtil.notBlank(orderId, "orderId is required.");
		return ordersGoodsService.getNotEvaluateOrderGoods(orderId);
	}
	
	
	@ApiOperation(value = "订单详情，微信支付")
	@RequestMapping(value = "/wechat/pay",method = RequestMethod.POST)
	public ResultVO wechatPay(HttpServletRequest request,String userId,String ordersId,String tradeType){
		ResultVO result = new ResultVO();
		AssertUtil.notNull(userId, "userId is required.");
		AssertUtil.notNull(ordersId, "orderId is required.");
		AssertUtil.notNull(tradeType, "tradeType is required.");
		result = ordersService.wechatPay(request, userId, ordersId, tradeType);
		return result;
	}
	
	@ApiOperation(value = "订单详情订单支付成功")
	@RequestMapping(value = "/wechat/pay/success",method = RequestMethod.GET)
	public ResultVO wechatPaySuccess(String shopOrderNumber){
		AssertUtil.notBlank(shopOrderNumber, "shopOrderNumber is required.");
		return ordersService.wechatPaySuccess(shopOrderNumber);
	}
	
	@ApiOperation(value = "物流跟踪")
	@RequestMapping(value = "/trace", method = RequestMethod.GET)
	public ResultVO logisticsTrace(String ordersId) {
		AssertUtil.notBlank(ordersId, "ordersId is required.");
		return ordersService.logisticsTrace(ordersId);
	}
	
	@ApiOperation(value = "确认订单")
	@RequestMapping(value = "/edit/status", method = RequestMethod.POST)
	public ResultVO editStatus(String shopOrderNumber,Integer status) {
		AssertUtil.notBlank(shopOrderNumber, "shopOrderNumber is required.");
		AssertUtil.notNull(status, "ordersId is required.");
		return ordersService.editStatusByShopOrderNumber(shopOrderNumber, status);
	}
	
	@ApiOperation(value = "删除线下支付失败订单")
	@RequestMapping(value = "/{orderNumber}", method = RequestMethod.DELETE)
	public ResultVO delOrders(@PathVariable String orderNumber) {
		AssertUtil.notBlank(orderNumber, "orderNumber is required.");
		return ordersService.delOrders(orderNumber);
	}
	
}
